<template>
  <div class="bg">
    <!-- 时间搜索框 -->
    <searchTime
      @searchvalue="userSearch"
      @searchreset="reset"
      :titMsg="titMsg"
      :options="options"
      :selectBoolen="false"
      :selectTime="true"
    />

    <!-- 表格展示 -->
    <el-card class="card">
      <div slot="header">
        <el-row type="flex" justify="end">
          <el-col :span="2">
            <el-button type="success" @click="exports" :loading="outExecl">导出数据</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格展示 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="index" width="100" label="序号" align="center"></el-table-column>
        <el-table-column prop="userName" label="用户昵称" align="center"></el-table-column>
        <el-table-column prop="userPhone" label="手机号码" align="center"></el-table-column>
        <el-table-column prop="goodsName" label="商品名称" align="center"></el-table-column>
        <!-- 展示对应的宠币提现收入情况 -->
        <el-table-column label="消费（宠币）" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.orderType==1?`+${scope.row.orderMoney}`:`-${scope.row.orderMoney}`}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="orderCreateTime" label="消费时间" align="center"></el-table-column>
        <el-table-column prop="userCurrencyBalance" label="余额" align="center"></el-table-column>
        <el-table-column label="订单状态" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.orderStatus==1?'待审核':scope.row.orderStatus==2?'已到账':'失败'}}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="total,  prev, pager, next, jumper"
        :total="total"
        :current-page="current"
        style="margin-top:24px"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { export2Excel } from "@/components/commom.js";
import searchTime from "@/components/searchTime";
export default {
  components: {
    searchTime
  },
  data() {
    return {
      outExecl: false, //导出
      titMsg: "请输入用户昵称/手机号码查询/商品名称",
      options: [
        {
          value: 1,
          label: "待审核"
        },
        {
          value: 2,
          label: "成功"
        },
        {
          value: 3,
          label: "失败"
        }
      ],
      value: "",
      value1: "",
      downloadLoading: false,
      pictLoading: true,
      current: 1, //页数
      total: 0, //页数
      orderStatus: 0, //查询状态
      alltabldata: [], //所有数据
      tableData: [] //宠物订单列表
    };
  },
  mounted() {
    this.petmoneylist();
  },
  methods: {
    // 获取列表数据
    petmoneylist() {
      let param = {
        size: 10,
        current: this.current,
        status: 2,
        startDate: this.startDate,
        endDate: this.endDate,
        selectCode: this.selectCode
      };
      this.$http.article.petmoney(param).then(res => {
        this.tableData = res.result.records;
        this.total = res.result.total;
        console.log("获取宠物订单列表", res);
        //显示每页展示的数量
        res.result.records.forEach((item, index) => {
          //res.data.data.list 表格数据     forEach为每一项添加数据
          item.index = index + 1 + (this.current - 1) * 10;
          // this.currentPage当前页  this.pageParams.size 每页显示多少    :page-sizes="[10,20,30,50]" 分页大小

          return item;
        });
      });
    },
    // 分页
    handleCurrentChange(val) {
      this.current = val;
      this.petmoneylist();
    },
    // 模糊查询
    userSearch(val) {
       this.current=1;//模糊查询
      this.orderStatus = val.value;
      this.startDate = val.firstTime;
      this.endDate = val.endTime;
      this.selectCode = val.search;
      this.petmoneylist();
    },
    // 重置功能
    reset(val) {
      this.orderStatus = "";
      this.startDate = "";
      this.endDate = "";
      this.selectCode = "";
      if (val === "reseta") this.petmoneylist();
    },
    // 导出数据
    exports() {
      this.outExecl = true;
      console.log("导出");
      this.petmoneylist();
      const hader = [
        {
          title: "用户昵称",
          date: "userName"
        },
        {
          title: "手机号码",
          date: "userPhone"
        },
        {
          title: "商品名称",
          date: "goodsName"
        },
        {
          title: "消费宠币",
          date: "orderType"
        },
        {
          title: "消费时间",
          date: "orderCreateTime"
        },
        {
          title: "余额",
          date: "userCurrencyBalance"
        },
        {
          title: "订单状态",
          date: "orderStatus"
        }
      ];

      const excelName = "宠物订单列表";
      var param = {
        current: 1,
        status: 2,
        size: this.total
      };
      // 获取全部数据列表
      this.$http.article.petmoney(param).then(res => {
        // this.alltabldata = res.result;
        const list = res.result.records.map(item => {
          //因为有枚举类型，所以这里对数据进行处理
          if (item.orderStatus) {
            item.orderType = item.orderType == 1 ? `+${item.orderMoney}` :  item.orderType == 2? `-${item.orderMoney}`:"";
            item.orderStatus =item.orderStatus == 1 ? "待审核" : item.orderStatus == 2 ? "已到账": "失败";
          }
          return item;
        });
        // console.log('获取状态内型',list)
        export2Excel(hader, list, excelName);
        setTimeout(() => {
          this.outExecl = false;
        }, 1000);
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.merchantData {
  display: flex;
  align-items: center;
  padding-top: 14px;
  .addmerchant {
    color: #409eff;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 400;
  }
  .merchantNum {
    color: #f23a3a;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 800;
  }
}

.clearfix {
  padding-bottom: 20px;
}
.bg {
  width: 100%;
  height: 100%;
  padding-top: 10px;
  background-color: #f4f7fd;
}

.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;
}
</style>